package com.yc.bos.web.action;


import com.yc.bos.model.Function;
import com.yc.bos.model.User;
import com.yc.bos.service.IFunctionService;
import com.yc.bos.utils.BOSContextUtils;
import com.yc.bos.web.action.base.BaseAction;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;

public class FunctionAction extends BaseAction<Function> {

    @Autowired
    private IFunctionService functionService;

    Logger logger = Logger.getLogger(FunctionAction.class);
    public void pageQuery() throws IOException {
        //1.接收参数 page[当前页] rows[每页显示多少条]
        //封装条件
        pd.setCurrentPage(Integer.parseInt(getModel().getPage()));
//        pd.setCurrentPage(page);
        pd.setPageSize(rows);
        System.out.println("functionAction-----"+page+"+"+rows);

        //2.调用service,参数里传一个PageBean
        functionService.pageQuery(pd);

        //3.返回json数据
        responseJson(pd,new String[]{"roles","currentPage","pageSize","detachedCriteria","function","functions"});

    }
    @Override
    public String save() {
        functionService.save(getModel());
        return NONE;
    }

    @Override
    public String update() {
        return null;
    }

    @Override
    public String delete() throws IOException {
        return null;
    }

    @Override
    public String list() {
        return null;
    }

    public void listJson() throws IOException {
        List<Function> functions=functionService.findAll();
        responseJson(functions,new String[]{"function","functions","roles"});
    }
    public void findMenu() throws IOException {
        List<Function> menu=null;
        User loginUser= BOSContextUtils.loginUser();
        if(loginUser.getUsername().equals("admin")){
            menu=functionService.findAllMenu();
        }else {
            menu=functionService.findMenuByUserId(loginUser.getId());
        }
        responseJson(menu,new String[]{"function","functions","roles"});

    }

}
